package com.damie.controller;


import com.damie.domain.WasteRating;
import com.damie.dto.*;
import com.damie.param.*;
import com.damie.service.WasteRatingService;
import com.damie.utils.Result;
import com.damie.utils.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Alon
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/damie/waste-rating")
@Api(description = "评价模块")
@Slf4j
public class WasteRatingController {

    @Autowired
    private WasteRatingService wasteRatingService;

    @ApiOperation(value = "评价订单")
    @PostMapping("/insertRating")
    public Result insertRating(@RequestBody WasteRatingDto wasteRatingDto, HttpServletRequest request){
        log.info("{}",wasteRatingDto);
        if (ObjectUtils.isEmpty(wasteRatingDto)){
            return new Result(false, StatusCode.NULL_ERROR,"订单数据不能为空");
        }
        WasteRatingParam wasteRatingParam = new WasteRatingParam();
        BeanUtils.copyProperties(wasteRatingDto,wasteRatingParam);
        Boolean b = wasteRatingService.insertRating(wasteRatingParam,request);
        if (b){
            return Result.success("评价成功");
        }
        return Result.fail("订单不存在");
    }

    @ApiOperation(value = "用户订单投诉")
    @PostMapping("/orderComplain")
    public Result orderComplain(@RequestBody WasteComplainDto wasteComplainDto){
        log.info("{}",wasteComplainDto);
        if (ObjectUtils.isEmpty(wasteComplainDto)){
            return new Result(false,StatusCode.NULL_ERROR,"参数不能为空");
        }
        WasteComplainParam wasteComplainParam = new WasteComplainParam();
        BeanUtils.copyProperties(wasteComplainDto,wasteComplainParam);
        Boolean complain = wasteRatingService.complain(wasteComplainParam);
        if (complain){
            return Result.success("投诉成功");
        }
        return Result.fail("单号不存在");
    }

    @ApiOperation(value = "用户追加评论")
    @PostMapping("/modifyComment")
    public Result modifyComment(@RequestBody CommentDto commentDto){
        log.info("{}",commentDto);
        if (ObjectUtils.isEmpty(commentDto)){
            return new Result(false,StatusCode.NULL_ERROR,"参数不能为空");
        }
        CommentParam commentParam = new CommentParam();
        BeanUtils.copyProperties(commentDto,commentParam);
        wasteRatingService.modification(commentParam);
        return Result.success("评论添加成功");
    }

    @ApiOperation(value = "删除评论")
    @GetMapping("/deletedComment")
    public Result deletedComment(String orderId){
        log.info("{}",orderId);
        if (StringUtils.isEmpty(orderId)){
            return Result.fail("没有放参数");
        }
        wasteRatingService.deletedComment(orderId);
        return Result.success("删除成功");
    }

    @ApiOperation(value = "站点回复评论")
    @PostMapping("/siteComment")
    public Result siteComment(@RequestBody ResultSiteDto resultSiteDto){
        log.info("{}",resultSiteDto);
        if (StringUtils.isEmpty(resultSiteDto)){
            return Result.fail("没有放参数");
        }
        ResultSiteParam resultSiteParam = new ResultSiteParam();
        BeanUtils.copyProperties(resultSiteDto,resultSiteParam);
        wasteRatingService.siteComment(resultSiteParam);
        return Result.success("回复成功");
    }

    @ApiOperation(value = "查看本站点的所有订单")
    @PostMapping("/selectSite")
    public Result selectSite(@RequestBody SitePageDto sitePageDto,HttpServletRequest request){
        log.info("{}",sitePageDto);
        if (StringUtils.isEmpty(sitePageDto)){
            return Result.fail("没有放参数");
        }
        SitePageParam sitePageParam = new SitePageParam();
        BeanUtils.copyProperties(sitePageDto,sitePageParam);
        List<WasteRating> wasteRatings = wasteRatingService.selectSite(sitePageParam,request);
        return Result.success(wasteRatings);
    }
}

